<template>
  <div class="container">
    <el-card class="box-card">
      <el-row>
        <el-col :span="22">
          <!-- 搜索、清除、新增 -->
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="学科名称">
              <el-input v-model="searchName"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="search()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-button type="success" @click="dialogVisible = true">
            <i class="el-icon-edit"></i> 新增学科</el-button
          >
        </el-col>
      </el-row>
      <el-alert :title="title" type="info" :closable="false" show-icon>
      </el-alert>
      <!-- 表格 -->
      <el-table style="width: 100%" :data="data">
        <el-table-column type="index" :index="indexMethod" label="序号"> </el-table-column>
         <el-table-column prop="subjectName" label="学科名称" width="180">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="120">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="180">
          <template #default="{row}">{{row.addDate|parseTimeByString()}}</template>
        </el-table-column>
        <el-table-column  label="前台是否显示">
          <template #default="{row}">{{row.isFrontDisplay===1?'是':'否'}}</template>
           </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录"> </el-table-column>
        <el-table-column prop="tags" label="标签" width="120"> </el-table-column>
        <el-table-column prop="totals" label="题目数量" width="120"> </el-table-column>

          <el-table-column  label="操作">
         <template #default="{row}">
          <el-button type="text" @click="$router.push(`directorys?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科分类</el-button>
          <el-button type="text" @click="$router.push(`tags?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科标签</el-button>
          <el-button type="text" @click="amend(row.id)">修改</el-button>
          <el-button type="text" @click="delremove(row.id)">删除</el-button>
           </template>
         </el-table-column>

      </el-table>
      <!-- 分页器 -->
   <div class="block">
     <el-pagination
     background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="getquery.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="getquery.pagesize"
      layout=" prev,pager, next,sizes,  jumper"
      :total="total">
    </el-pagination>
  </div>

  <!-- 对话框 -->
     <el-dialog
      :title="fromtitle"
      :visible.sync="dialogVisible"
      :before-close="Close"
      width="30%">

     <el-form :model="getquery" ref="dateForm" label-width="100px" :rules="rules"  class="demo-ruleForm">
  <el-form-item
   prop="subjectName"
    label="学科名称"
  >
    <el-input type="age" v-model="getquery.subjectName" placeholder="请输入学科名称"></el-input>
  </el-form-item>
  <!-- 滑块开关 -->
  <el-form-item prop="isFrontDisplay" label="是否显示">
        <el-switch
        style="display: block"
        :active-value="1"
        :inactive-value="0"
        v-model="isFrontDisplay"
        active-color="#13ce66"
        inactive-color="#ff4949"
      >
       </el-switch>

  </el-form-item>

  </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="Close()">取 消</el-button>
        <el-button type="primary" @click="addform()">确 定</el-button>
       </span>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, remove, detail, update } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      data: [],
      pages: 0,
      title: '',
      total: 0,
      id: '',
      dialogVisible: false,
      isFrontDisplay: 1,
      searchName: '',

      getquery: {
        pagesize: 10,
        page: 1,
        subjectName: ''
      },
      rules: {
        subjectName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]

      },
      fromtitle: '新增学科'
    }
  },
  created () {
    this.getList()
  },
  computed: {
    isFrontDis: {
      get () {
        return this.data.isFrontDisplay
      },
      set (val) {
        this.data.isFrontDisplay = val
      }

    }
  },
  methods: {
    // 渲染页面
    async getList () {
      const { data } = await list(this.getquery)
      this.title = `数据一共${data.counts}条`
      this.data = data.items
      console.log(this.data)
      this.isFrontDis = data.items.isFrontDis
      this.total = data.counts
    },
    // 清除和搜索
    async search () {
      const { data } = await list({ pagesize: this.getquery.pagesize, page: this.getquery.page, subjectName: this.searchName })
      console.log(data)
      this.data = data.items
    },
    clear () {
      this.searchName = ''
    },
    // 分页器
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.getquery.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      console.log(val)
      this.getquery.page = val
      this.getList()
    },
    indexMethod (index) {
      return index + 1 + (this.getquery.page - 1) * this.getquery.pagesize
    },
    // 对话框
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 新增学科-校验
    addform () {
      // 预校验
      this.$refs.dateForm.validate(async valid => {
        if (!valid) return
        if (valid) {
          if (this.fromtitle === '新增学科') {
            const { data } = await add({ isFrontDisplay: this.isFrontDisplay, subjectName: this.getquery.subjectName })
            this.id = data
            this.getquery.page = 1
            this.getList()

            this.dialogVisible = false
          }
          if (this.fromtitle === '修改学科') {
            // 发送修改请求
            await update({ id: this.id, subjectName: this.getquery.subjectName, isFrontDisplay: this.isFrontDisplay })
            // this.getquery.subjectName = ''
            this.dialogVisible = false
            this.getList()
          }
          this.getquery.subjectName = ''
        }
      })
    },
    // 关闭对话框
    Close () {
      this.getquery.subjectName = ''
      this.isFrontDisplay = 1
      this.dialogVisible = false
    },
    // 删除
    delremove (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      }).then(async () => {
        await remove({ id: row })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(row)
    },
    // 修改
    async amend (row) {
      // 回显
      this.fromtitle = '修改学科'
      this.dialogVisible = true
      this.id = row
      const { data } = await detail({ id: this.id })
      this.getquery.subjectName = data.subjectName
      this.isFrontDisplay = data.isFrontDisplay
      console.log(this.isFrontDisplay)
    }

  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-form-item__label{
  text-align: left;
}
::v-deep .el-switch{
  line-height: 33px;
}
::v-deep .el-pagination{
  text-align: right;
  margin-top: 20px;
}
</style>
